// Geometric Tools, Inc.
// http://www.geometrictools.com
// Copyright (c) 1998-2006.  All Rights Reserved
//
// The Wild Magic Library (WM3) source code is supplied under the terms of
// the license agreement
//     http://www.geometrictools.com/License/WildMagic3License.pdf
// and may not be copied or disclosed except in accordance with the terms
// of that agreement.

#include "Loader3ds.h"
using namespace Wm3;

#include <iostream>
using namespace std;

//----------------------------------------------------------------------------
// To convert from 3D Studio Max's 3ds format to Wild Magic's wmof format:
//   3dsToWmof model.3ds model.wmof
//
// Any texture images needed by the 3ds file must be located in the same
// directory as the 3ds file, must have been converted to a .wmif format
// image, and must replace the old image filename extension with .wmif.
//----------------------------------------------------------------------------
int main (int iQuantity, char** aacArgument)
{
    if (iQuantity != 3)
    {
        return -1;
    }

    Loader3ds kLoader;
    if (!kLoader.LoadFile(aacArgument[1]))
    {
        return -2;
    }

    // SBW modifying to get the root scene name from the file, so objects in
    // LTM can be identified better than calling everything "3DSroot"

    string rootName = aacArgument[1];
    unsigned int lastSlashPos = -1;
    for (unsigned int i=0; i<rootName.length(); i++) {
      if (rootName[i] == '/' || rootName[i] == '\\') {
        // need to verify this works in Windows..
        lastSlashPos = i;
      }
    }
    // start at the position after the last slash, get all characters except
    // the last .3ds
    rootName = rootName.substr(lastSlashPos + 1, rootName.length() - lastSlashPos - 5);

    NodePtr spkScene = kLoader.LoadSceneGraph(true, rootName);

    Stream kOStream;
    kOStream.Insert(spkScene);
    kOStream.Save(aacArgument[2]);

#ifdef WM3_MEMORY_MANAGER
#ifdef _DEBUG
    Memory::GenerateReport("MemoryReportDebug.txt");
#else
    Memory::GenerateReport("MemoryReportRelease.txt");
#endif
#endif

    return 0;
}
//----------------------------------------------------------------------------

